package com.kyzh.core.pager.weal.share

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.gushenge.atools.util.ADate
import com.gushenge.atools.util.AView
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Share
import com.gushenge.core.requests.WealRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.kyzh.core.databinding.ActivityShareBinding
import com.kyzh.core.databinding.ItemShareCodeBinding
import com.kyzh.core.dialog.showDescDialog
import com.kyzh.core.utils.*
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import com.umeng.socialize.shareboard.ShareBoardConfig
import kotlinx.android.synthetic.main.activity_share.*
import org.jetbrains.anko.toast

class ShareActivity : BaseActivity() {
    private lateinit var binding:ActivityShareBinding
    lateinit var mNote:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_share)
        AView.setStatusBar(this, false)
        binding.data = Share()
        initData()
        tvShareRules.setOnClickListener {
            mNote?.let {
                showDescDialog(getString(R.string.activityRules),it){}
            }
        }
    }

    private fun initData() {
//        if(packageName !="a94hwan.bjkyzh.star"  &&(MyApplication.qqzone == "101559064" || MyApplication.weixin == "wx928808eb5354008a")){
        if(showShare()){
            btShare.visibility = View.VISIBLE
        }else{
            btShare.visibility = View.GONE
        }



//        }
        WealRequest.share {
            mNote=note
            binding.data = this
            val codes  = arrayListOf<String>()
            for (i in invite_code.indices){
                codes.add(invite_code.substring(i, i + 1))
            }
            rvCode.layoutManager = object :LinearLayoutManager(this@ShareActivity, RecyclerView.HORIZONTAL, false){
                override fun canScrollHorizontally(): Boolean {
                    return false
                }

                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            rvCode.adapter = Adapter(R.layout.item_share_code, codes)

            tvCopy.setOnClickListener {
                invite_code.copy()
                getString(R.string.copySuccess).toast()
            }
            btShare.setOnClickListener {
                if(showShare()){
                val config = ShareBoardConfig()
                config.setMenuItemBackgroundShape(ShareBoardConfig.BG_SHAPE_CIRCULAR)
                config.setCancelButtonBackground(Color.rgb(240, 244, 246))
                config.setShareboardBackgroundColor(Color.rgb(240, 244, 246))
                config.setIndicatorVisibility(false)
                config.setTitleText(getString(R.string.shareGame))
                val image = UMImage(this@ShareActivity, R.drawable.logo_new)
                val web = UMWeb(link)
                web.title = getString(R.string.app_name)
                web.setThumb(image)
                web.description = getString(R.string.findGameFun)
                ShareAction(this@ShareActivity)
                        .withMedia(web)
                        .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                        .setCallback(shareListener)
                        .open(config)
                val createQRImage = createQRImage(link)
                val image1 = UMImage(this@ShareActivity, createQRImage)
                image1.setThumb(image)
                ShareAction(this@ShareActivity).withMedia(image1)
                        .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                        .setCallback(shareListener)
                        .open(config)
                }else{

                val createQRImage = createQRImage(link)
                val view = LayoutInflater.from(this@ShareActivity).inflate(R.layout.dialog_share, null)
                val code = view.findViewById<ImageView>(R.id.img_code)
                val save = view.findViewById<TextView>(R.id.tv_save)
                code.setImageBitmap(createQRImage)
                save.setOnClickListener {
                    if(createQRImage!=null)
                        saveResource(createQRImage,time){
                            getString(R.string.saveSuccess).toast()
                        }
                }
                val bottomSheetDialog = BottomSheetDialog(this@ShareActivity)
                bottomSheetDialog.setContentView(view)
                bottomSheetDialog.show()
                }
            }
            ivQrcode.setOnLongClickListener {
                ivQrcode.buildDrawingCache(true)
                val bitmap: Bitmap = ivQrcode.getDrawingCache(true)
                saveResource(bitmap,ADate.getStamp().toString()){
                    getString(R.string.saveSuccess).toast()
                }
                return@setOnLongClickListener true
            }
            btShareHistory.setOnClickListener {
                startActivity<ShareHistoryActivity>()
            }
        }
    }
    private inner class Adapter(layout: Int, beans: ArrayList<String>):BaseQuickAdapter<String, BaseDataBindingHolder<ItemShareCodeBinding>>(layout, beans){
        override fun convert(holder: BaseDataBindingHolder<ItemShareCodeBinding>, item: String) {
            holder.dataBinding?.data = item
        }
    }


    private val shareListener = object : UMShareListener {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        override fun onStart(platform: SHARE_MEDIA) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        override fun onResult(platform: SHARE_MEDIA) {
            toast(getString(R.string.shareSuccess))
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        override fun onError(platform: SHARE_MEDIA, t: Throwable) {
            toast(getString(R.string.shareFailed))
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        override fun onCancel(platform: SHARE_MEDIA) {
            toast(getString(R.string.shareCancel))

        }
    }

}